package core.validate;

public class BooleanRule extends Rule<BooleanRule> {
    public BooleanRule(String key) {
        super(key);
        actions.add(holder -> {
            boolean status = true;
            Object value = holder.getValue();
            if(value instanceof Number){
                value = value.toString();
            }

            if(value instanceof String){
                String strValue = ((String) value).toLowerCase();
                if(strValue.equals("yes") || strValue.equals("1") || strValue.equals("true") || strValue.equals("on")){
                    value = true;
                }else if(strValue.equals("no") || strValue.equals("0") || strValue.equals("-1") || strValue.equals("false") || strValue.equals("off")){
                    value = false;
                }
            }

            if(!(value instanceof Boolean)){
                status = false;
            }

            holder.setValue(value).setStatus(status).setMessage(getMessage("无效的值"));
        });
    }

    protected BooleanRule inst() {
        return this;
    }
}